from django.shortcuts import render

# Create your views here.
from rest_framework import viewsets
from users.models import MyUser, Position
from users.serializers import UserSerializer, PositionSerializer, UserInfoStockSerializer
from django.db.models import Q
from rest_framework import mixins


class UserViewSet(viewsets.ModelViewSet):
    queryset = MyUser.objects.all()
    serializer_class = UserSerializer


class UserInfo(mixins.ListModelMixin, viewsets.GenericViewSet):
    serializer_class = UserInfoStockSerializer
    """:arg
    
    """
    def get_queryset(self):
        """
        返回自己的
        """
        return MyUser.objects.filter(id=self.request.user.id)


class PositionView(viewsets.ModelViewSet):
    """
        当前所持有的股票信息
    """
    serializer_class = PositionSerializer

    def get_queryset(self):
        """
        管理员返回所有的 非管理员返回自己的
        """
        if self.request.user.is_superuser:
            return Position.objects.all()
        if self.request.query_params.get('stock'):
            return Position.objects.filter(
                Q(user_id=self.request.user.id) & Q(stock=self.request.query_params.get('stock')))
        return Position.objects.filter(user_id=self.request.user.id)
